package first.group.ssh.packet;

import first.group.ssh.SSHConsts;
import first.group.ssh.packet.reader.StringReader;
import first.group.ssh.packet.writer.StringWriter;
import java.io.IOException;
import java.nio.ByteBuffer;

public class ServiceAcceptPacket extends Packet {

    public final String serviceName;
    
    public ServiceAcceptPacket(String serviceName) {
        super(SSHConsts.SSH_MSG_SERVICE_ACCEPT);
        this.serviceName = serviceName;
    }

    public static PacketParser<ServiceAcceptPacket> PARSER = new Parser();

    private static class Parser extends PacketParser<ServiceAcceptPacket> {

        @Override
        public ServiceAcceptPacket parse(ByteBuffer buffer) throws IOException {
            var serviceName = StringReader.readString(buffer);
            if (serviceName == null) {
                return null;
            }
            return new ServiceAcceptPacket(serviceName);
        }
    }

    @Override
    public void write(ByteBuffer buffer) {
        StringWriter.writeString(buffer, serviceName);
    }
}
